
package codeGenerate.front.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;

import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.xml.sax.SAXException;

import codeGenerate.common.utils.SystemUtils;
import codeGenerate.custom.tempalte.TemPathCustom;
import codeGenerate.front.generater.FrontGenerater;
import codeGenerate.front.vo.FieldVo;
import codeGenerate.front.vo.FrontConfigVo;

public class FrontGenUtils {

	/**
	 * @description: 配置文件路径
	 * @creator: dw-fu1
	 * @createDate: 2018年2月26日 
	 * @modifier:
	 * @modifiedDate:
	 * @param configPath 配置路径
	 * @throws Exception
	 */
	public static void genCode(String configPath) throws Exception {
		File file = new File(configPath);
		if (!file.exists()) {
			System.out.println("文件不存在：" + configPath);
		}
		FileInputStream is = new FileInputStream(file);
		genCode(is);
	}

	/**
	 * @description: 流
	 * @creator: dw-fu1
	 * @createDate: 2018年2月26日 
	 * @modifier:
	 * @modifiedDate:
	 * @param is
	 * @throws Exception
	 */
	public static void genCode(InputStream is) throws Exception {
		List<FrontConfigVo> configList = readConfig(is);
		for (FrontConfigVo cf : configList) {
			FrontGenerater gen = new FrontGenerater();
			gen.setConfigVo(cf);
			gen.generateCode();
		}
	}

	@SuppressWarnings("unchecked")
	public static List<FrontConfigVo> readConfig(InputStream is) throws ParserConfigurationException, SAXException, IOException, DocumentException {
		try {
			List<FrontConfigVo> configVoList = new ArrayList<>();
			//创建解析器
			SAXReader reader = new SAXReader();
			//得到document
			Document document = reader.read(is);
			Element root = document.getRootElement();
			String createUser = root.elementText("createUser");
			String copyright = root.elementText("copyright");
			String template = root.elementText("template");
			String genPath = root.elementText("genPath");
			String templateDirectory = root.elementText("templateDirectory");
			if (StringUtils.isBlank(template)) {
				template = TemPathCustom.class.getName();
			}
			if (StringUtils.isBlank(templateDirectory)) {
				templateDirectory = SystemUtils.defalutTemplateDirectory();
			}
			List<Element> beanEles = root.element("beans").elements("bean");
			for (Element beanEle : beanEles) {
				FrontConfigVo cv = new FrontConfigVo();
				cv.setCreateUser(createUser);
				cv.setCopyright(copyright);
				cv.setTemplate(template);
				cv.setGenPath(genPath);

				cv.setTemplateDirectory(templateDirectory);
				cv.setCreateTime(new Date());
				cv.setBeanDescription(beanEle.attributeValue("desc"));
				cv.setBeanName(beanEle.attributeValue("beanName"));
				// 列表查询条件
				List<FieldVo> listSearchFields = new ArrayList<>();
				List<Element> listSearchFieldEles = beanEle.selectNodes("list/search/field");
				for (Element element : listSearchFieldEles) {
					listSearchFields.add(toField(element));
				}
				cv.setListSearchFields(listSearchFields);
				//列表表格字段
				List<FieldVo> listTableFields = new ArrayList<>();
				List<Element> listTableFieldEles = beanEle.selectNodes("list/table/field");
				for (Element element : listTableFieldEles) {
					listTableFields.add(toField(element));
				}
				cv.setListTableFields(listTableFields);
				//表单
				List<FieldVo> formFields = new ArrayList<>();
				List<Element> formFieldEles = beanEle.selectNodes("form/field");
				for (Element element : formFieldEles) {
					formFields.add(toField(element));
				}
				cv.setFormFields(formFields);
				configVoList.add(cv);
			}
			return configVoList;
		} finally {
			is.close();
		}

	}

	private static FieldVo toField(Element ele) {
		FieldVo vo = new FieldVo();
		vo.setTitle(ele.attributeValue("title"));
		vo.setType(ele.attributeValue("type"));
		vo.setName(ele.attributeValue("name"));
		vo.setValidate(ele.attributeValue("validate"));
		vo.setOption(StringUtils.defaultIfBlank(ele.getTextTrim(), "{}"));
		return vo;
	}

	public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, DocumentException {
		File file = new File("C:\\Users\\Administrator\\git\\codeGenerate\\src\\main\\java\\codeGenerate\\front\\utils\\jdbc2.xml");
		FileInputStream is = new FileInputStream(file);
		readConfig(is);
	}
}
